
#ifndef  KEY_C  
#define  KEY_C

#include "key_process.h"
#include "soft_time.h"
#include "comm_wifi.h"
#include "..\Drivers\SCDriver_list.h"
#include "HeadFiles\SysFunVarDefine.h"
/*-----------------*
 |  Private Data
 *-----------------*/



#define ADD_KEY   3//?????
#define KEY_NUM   7
#define ELEMENT_NUMBER	 (KEY_NUM+ADD_KEY)//9
#define KEY_UP           0
#define KEY_PRESS_SHORT  1
#define KEY_PRESS_LONG   2
//用来判断在是否存在�?�个按键�?按下，不包括组合�?�?
#define KEY_TIME_PRESS   20
#define KEY_PRESS_LONG_TIME           150//300//1.5S
#define KEY_PRESS_LONG_TIME_2S        200//600//3S
#define KEY_PRESS_LONG_TIME_5S        500//1000//5S





#define  KEY_counts          5
#define  KEY_up_conuts       5


static code uint16_t Key_Map[ELEMENT_NUMBER]={
   // 0,
	KEY_POWER,
	KEY_INCREASE,
	KEY_DECREASE,
 	KEY_ECO,
	KEY_CYCLE,
	KEY_MODE,
	KEY_PREUSS,
	
	KEY_INCREASE+KEY_DECREASE, //buzzer
	KEY_POWER+KEY_ECO,   //WIFI
	KEY_CYCLE+KEY_ECO//version
};
struct Key_Info 
{
    uint32_t key_mask;
    uint16_t  ke_stat[ELEMENT_NUMBER];
    uint16_t  key_count[ELEMENT_NUMBER];
    uint16_t  key_up_count[ELEMENT_NUMBER];
};

struct Key_Info key_info;

static KEY_NAMES key_Event_Id=KEY_NONE;
static uint8_t key_Same=0;
static KEY_NAMES key_Event_Id_Old=KEY_NONE;
static uint8_t button_stat=0;
extern Time one_minute_version;
//uint8_t Led_Button=breath_led;
//static
//uint16_t times=0;
//uint8_t led_t1=0;
void key_process_init(void)
{
		uint8_t i;
	key_info.key_mask=0;
	for(i=0;i<ELEMENT_NUMBER;i++)
	{
			key_info.ke_stat[i]=0;
			key_info.key_count[i]=0;
			key_info.key_up_count[i]=KEY_up_conuts;
			//key_info.key_mask=0;
	}
}
//static KEY_NAMES   keyEvent;      /* signal keyEvent, indicate what key is pressed */
uint8_t key_cnt_test=0;
void Button_stat(void)
{
		button_stat=0;
		if(exKeyValueFlag==0)
		{
			button_stat=0;
		}
		if(exKeyValueFlag&0x00000800)
		{
			
			button_stat|= KEY_POWER;
		}
		if(exKeyValueFlag&0x00000100)
		{
			
			button_stat|= KEY_INCREASE;
		}
		if(exKeyValueFlag&0x00001000)
		{
			
			button_stat|= KEY_DECREASE;
		}
		if(exKeyValueFlag&0x00000040)
		{
			
			button_stat|= KEY_CYCLE;
			//button_stat|= KEY_POWER;
		}
		if(exKeyValueFlag&0x00002000)
		{
			
			button_stat|= KEY_ECO;
		}
		if(exKeyValueFlag&0x00000020)
		{
			
			button_stat|= KEY_MODE;
			//button_stat|= KEY_POWER;
		}
		if(exKeyValueFlag&0x00000400)
		{
			
			button_stat|= KEY_PREUSS;
			//button_stat|= KEY_POWER;
		}
}
 /*---------------------------------------------------------------------------*
 |  KeyInit
 |
 |  Initialize the key module. Called once on power-up.
 |
 |  For details see Key.h
 *---------------------------------------------------------------------------*/
void KeyInit(void)
{
    button_stat=0;
}

void Event_Clean(void)
{
    uint8_t i;
    
        key_Event_Id=KEY_NONE;
        key_Event_Id_Old=KEY_NONE;
        key_Same=0;
        for(i=0;i<(ELEMENT_NUMBER);i++)  {
            key_info.ke_stat[i]=KEY_UP;
        }
}

/*---------------------------------------------------------------------------*
 |  KeyEvent
 |
 |  For details see Key.h
 *---------------------------------------------------------------------------*/


uint8_t KeyEvent(void)
{
    uint8_t event_id=0;

    uint8_t i;
    for(i=0;i<(ELEMENT_NUMBER);i++){
        //�?有长按和�?按进行判�?，其他情况不进�?��?�理
    	if(key_info.ke_stat[i]==KEY_PRESS_SHORT||key_info.ke_stat[i]==KEY_PRESS_LONG) 
			event_id= 2*i+ key_info.ke_stat[i];//key_info.ke_stat ???? 0,1,2
    }
    return event_id; 
}

/*---------------------------------------------------------------------------*
 |  KeyKeyProcess
 |
 |  Called once per line sync.  For details see Key.h
 *---------------------------------------------------------------------------*/
void KeyProcess(void)
{
    uint8_t i;
    uint8_t j;
    //static uint32_t key_mask_old=0;
	Button_stat();//????
	// ???		??1		??2			??3	     ??1+??3		??4			??5				??2+??3||(button_stat==0x06)
	// ???		??1		??2			??3	     ??1+??3		??4			??5				??2+??3||(button_stat==0x06)
	if((button_stat==0)||(button_stat==1)||(button_stat==2)||(button_stat==0x4)||(button_stat==0x08)||(button_stat==0x10)||(button_stat==0x20)||(button_stat==0x40))
		{
		
		key_info.key_mask = button_stat;//KeyMatrixLevel();????????   /* get key press reading */

	}
	else if((button_stat==Key_Map[8])&&(key_info.key_count[0]<KEY_PRESS_LONG_TIME)&&(key_info.key_count[3]<KEY_PRESS_LONG_TIME))
	{
		key_info.key_mask = button_stat;//
		key_info.ke_stat[0]=KEY_TIME_PRESS;
		key_info.ke_stat[3]=KEY_TIME_PRESS;
	}
	else if((button_stat==Key_Map[7])&&(key_info.key_count[1]<KEY_PRESS_LONG_TIME)&&(key_info.key_count[2]<KEY_PRESS_LONG_TIME))
	{
		key_info.key_mask = button_stat;//
		key_info.ke_stat[1]=KEY_TIME_PRESS;
		key_info.ke_stat[2]=KEY_TIME_PRESS;
	}else if((button_stat==Key_Map[9])&&(key_info.key_count[2]<KEY_PRESS_LONG_TIME)&&(key_info.key_count[1]<KEY_PRESS_LONG_TIME))
	{
		key_info.key_mask = button_stat;//
		key_info.ke_stat[1]=KEY_TIME_PRESS;
		key_info.ke_stat[2]=KEY_TIME_PRESS;
	} else
    {
        //组合�?情况，当前按下，但是并不�?设定组合�?，��且组合�?其中丢��?按键已经进�?�了长按�?
        for(i=0;i<(ELEMENT_NUMBER);i++)
        {
            //找出朢�先按下的按键，最先按下的，最先到达长按时�?
            if(((button_stat&Key_Map[i])!=0)&&(key_info.ke_stat[i]==2))
            {
                //再�?�遍历，找出后面按下的，为了防�?�后面按下了两三�?，所以在这里不进行跳出，
                for(j=0;j<(ELEMENT_NUMBER);j++)
                {
                    if((i!=j)&&((button_stat&Key_Map[j])!=0))
                    {
                        key_info.key_count[j]=0; 
                        //把它设置成这�?标�?�位
                        key_info.ke_stat[j]=KEY_TIME_PRESS;
                    }
                }
            }
        }
        
    }

    for(i=0;i<ELEMENT_NUMBER;i++)//20mS
	{
    	if(key_info.key_mask == Key_Map[i]){ 
            //存在�?暂的拉高
            if(key_info.key_up_count[i]<KEY_up_conuts&&key_info.key_up_count[i]>0)
            {
                //出现丢�次很�?的按下，然后�?起，�?起时间也很短
                //按下次数小于�?次，小于40MS，马上又�?起了
                if(key_info.key_count[i]<KEY_counts)
                {
                    key_info.key_count[i]=0;
                }
                key_info.key_up_count[i]=0;
            }
            //当前�?丢�次进来，上拉计数清空�?
            if(key_info.key_count[i]==0)
            {
                key_info.key_up_count[i]=0;
            }
            if(key_info.ke_stat[i]!=KEY_TIME_PRESS)
            {
            	key_info.key_count[i]++;
            }
            if((i==1||i==2)&&key_info.key_count[i]>=KEY_PRESS_LONG_TIME)
            {
            	key_info.key_count[i]=KEY_PRESS_LONG_TIME;
            }
            else if((i>=3&&i<ELEMENT_NUMBER)&&key_info.key_count[i]>=KEY_PRESS_LONG_TIME_2S)
			{
				key_info.key_count[i]=KEY_PRESS_LONG_TIME_2S;
			}
            else if((i==0)&&key_info.key_count[i]>=KEY_PRESS_LONG_TIME_5S)
			{
				key_info.key_count[i]=KEY_PRESS_LONG_TIME_5S;
			}
        }
        else{//????
				//两�?�情况，1�?，组合键，清掉分�?的�?�数�?2�?，后按的按键标�?�数�?清理�?
				if((i<KEY_NUM)&&((key_info.key_mask&Key_Map[i])!=0))//?????,??????
				{
					key_info.key_count[i]=0;
					key_info.ke_stat[i]=KEY_UP;
				}
				//当前这个按键处于上拉状��?
				key_info.key_up_count[i]++;
				//朢�大五�?
				if(key_info.key_up_count[i]>=KEY_up_conuts)
				{
					key_info.key_up_count[i]=KEY_up_conuts;
					//假�?�存在短暂拉低��?
					if(key_info.key_count[i]<KEY_counts)
					{
						key_info.key_count[i]=0;
					}
					if(key_info.ke_stat[i]>0)//||key_info.ke_stat[i]==0
					{
						key_info.ke_stat[i]=KEY_UP;
						key_info.key_count[i]=0;
					}
					else
					{
						if((i>=KEY_NUM)&&(key_info.key_count[i]>=KEY_counts)&&(key_info.key_count[i]<KEY_PRESS_LONG_TIME_2S))
						{
							key_info.ke_stat[i]=KEY_UP;
							key_info.key_count[i]=0;
							for(j=0;j<KEY_NUM;j++)
							{
								if((Key_Map[i]&Key_Map[j])!=0)
								{
									key_info.key_count[j]=0; 
									//把它设置成这�?标�?�位
									key_info.ke_stat[j]=KEY_TIME_PRESS;
								}
							}

						}
					}
				}
				
				
           }
                              
    }

}
void button_process(void)
{
	uint8_t i;
	for(i=0;i<ELEMENT_NUMBER;i++)
	{
		if(i>=KEY_NUM)
		{
			//组合按键�?3S有效
			if(key_info.key_count[i]>=KEY_PRESS_LONG_TIME_2S)
			{
				key_info.ke_stat[i]=KEY_PRESS_LONG;
			}
		}
		else if(i<KEY_NUM)
		{
			if((i==0)||(i==3))
			{
				//按下�?起有效，�?存在于短�?
				if((key_info.key_count[i]>=KEY_counts)&&(key_info.key_count[i]<KEY_PRESS_LONG_TIME)&&(key_info.key_up_count[i]==KEY_up_conuts))
				{
					key_info.ke_stat[i]=KEY_PRESS_SHORT;
				}
				else if((key_info.key_count[i]>=KEY_PRESS_LONG_TIME))//1.5S长按&&(key_info.key_up_count[i]==KEY_up_conuts)
				{
					key_info.ke_stat[i]=KEY_PRESS_LONG;
				}
			}
			else
			{
				if((key_info.key_count[i]>=KEY_counts)&&(key_info.key_count[i]<KEY_PRESS_LONG_TIME))
				{
					key_info.ke_stat[i]=KEY_PRESS_SHORT;
				}
				else if(key_info.key_count[i]>=KEY_PRESS_LONG_TIME)
				{
					key_info.ke_stat[i]=KEY_PRESS_LONG;
				}
			}
		}
	}
	//进�?�赋�?
    key_Event_Id=(KEY_NAMES)KeyEvent();
    if( key_Event_Id_Old==key_Event_Id) key_Same=1;
    else key_Same=0;
    key_Event_Id_Old=key_Event_Id;
    
    /* 新的按键按下, 3秒钟内屏蔽wifi的命令 */
    if(key_Event_Id != 0 && key_Same==0)
    {
        three_3_flg=1;
    }
}
/*---------------------------------------------------------------------------*
 |  KeyBufferClear
 |
 |  Clear buffer of all key presses.  For details see Key.H
 *---------------------------------------------------------------------------*/
uint8_t EventListener( KEY_NAMES  keyEvent)
{
//    if(key_Event_Id_Old==key_Event_Id)  return 0;
    
    if(keyEvent==KEY_NONE && (key_Event_Id==KEY_NONE || key_Same==1) )  
	{
        
//        if( key_Same==1)  DBUG_PutString(" key_Same \r\n");
        return 1;   
    }
    
    if(keyEvent==key_Event_Id && key_Same==0) 
	{
        return 1;
    }
    if((keyEvent==key_Event_Id) && ((key_Event_Id==KEY_INCREASE_LONG)||(key_Event_Id==KEY_DECREASE_LONG))) {

        return 1;
    }
//    if((keyEvent==key_Event_Id) && ((key_Event_Id==KEY_HEAT_WATER_INCREASE_LONG)||(key_Event_Id==KEY_HEAT_WATER_DECREASE_LONG))) {
//        return 1;
//    }
    return 0;
}
//extern uint16_t num;

uint16_t GetKeyboard(void){
	return button_stat;
}

#endif



